package com.woc.boot.admin.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woc.boot.admin.constant.ErrorCode;
import com.woc.boot.admin.entity.Response;
import com.woc.boot.srv.model.WocSeo;
import com.woc.boot.srv.service.WocSeoService;

/**
 * SEO管理控制器
 * 
 * @author     woc
 * @date       2024-01-26 
 * @version    0.0.1
 */
@RestController
@RequestMapping(value = "/seo")
public class SeoController {

    @Autowired
    private WocSeoService wocSeoService;

    @RequestMapping(value = {"", "/index"}, method = RequestMethod.GET)
    public Object index(@RequestParam(required = false, defaultValue = "0") Integer sid) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("sid", sid);
        mav.setViewName("seo/index");
        return mav;
    }

    @RequestMapping(value = {"/list"}, method = RequestMethod.GET)
    public Object list(@RequestParam(required = false, defaultValue = "0") Integer sid,
            @RequestParam(required = false, defaultValue = "0") Integer offset,
            @RequestParam(required = false, defaultValue = "10") Integer limit) {
        QueryWrapper<WocSeo> wrapper = new QueryWrapper<>();
        if (sid > 0) {
        	wrapper.eq("sid", sid);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("total", wocSeoService.count(wrapper));
        wrapper.orderByDesc("id");
        wrapper.last(" limit " + offset + "," + limit);
        result.put("rows", wocSeoService.list(wrapper));
        return result;
    }

    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ResponseBody
    public Response<Object> refresh(Integer id) {
        Response<Object> resp = new Response<>();
        if (id == null || id <= 0) {
            return resp.withError(ErrorCode.ParamsError);
        }
        return resp.withOk();
    }

}
